import datetime
import logging
import time
import uuid
from datetime import datetime
from typing import Union, Optional, Dict, Any

import pymysql
from dbutils.pooled_db import PooledDB
from pymysql.cursors import DictCursor
from scrapy.utils.project import get_project_settings
import requests
from requests.auth import HTTPBasicAuth
import base64


class MySQLPool:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance.init_pool()
        return cls._instance

    def init_pool(self):
        settings = get_project_settings()
        self.pool = PooledDB(
            creator=pymysql,
            host=settings.get('MYSQL_HOST'),
            port=settings.get('MYSQL_PORT', 3306),
            user=settings.get('MYSQL_USER'),
            password=settings.get('MYSQL_PASSWORD'),
            db=settings.get('MYSQL_DATABASE'),
            charset='utf8mb4',
            cursorclass=DictCursor,
            mincached=5,  # 初始空闲连接数
            maxcached=20,  # 最大空闲连接数
            maxconnections=100,  # 最大连接数
            blocking=True,  # 连接数达到最大时阻塞等待
            ping=1,  # 检查连接是否可用
        )

    def get_conn(self):
        return self.pool.connection()

    def close(self):
        self.pool.close()


class DateProcess:

    def __init__(self):
        pass

    @staticmethod
    def get_past_report_dates(self):
        """获取已过去的报告日期"""
        now = datetime.datetime.now()
        dates = []

        for year in range(now.year, now.year - 4, -1):
            # 检查每个季度是否已过
            if year < now.year or (year == now.year and now.month > 3):
                dates.append(f"{year}-03-31")
            if year < now.year or (year == now.year and now.month > 6):
                dates.append(f"{year}-06-30")
            if year < now.year or (year == now.year and now.month > 9):
                dates.append(f"{year}-09-30")
            if year < now.year or (year == now.year and now.month > 12):
                dates.append(f"{year}-12-31")

        return dates

    @staticmethod
    def str_date_to_timestamp(date_str):
        dt = datetime.datetime.strptime(date_str, '%Y-%m-%d')
        return str(int(dt.timestamp() * 1000))

    @staticmethod
    def get_timestamp():
        return str(int(time.time() * 1000))


class DorisPool:
    def __init__(self, host='172.26.245.203', port=8030, db='xueqiu_data', user='root', password=''):
        self.url = f'http://{host}:{port}/api/{db}/{{table}}/_stream_load'
        self.auth = HTTPBasicAuth(user, password)
        self.session = requests.Session()
        self.session.should_strip_auth = lambda *_: False

    def load(self, table, data, format='json', sep=','):
        resp = self.session.put(
            url=self.url.format(table=table),
            data=data,
            headers={
                'Content-Type': 'application/json',
                'label': f'batch_{uuid.uuid4()}',
                'format': format,
                'column_separator': sep
            },
            auth=self.auth
        )
        return resp.json()


















